﻿using System.Collections.Generic;
using System.Linq;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.BusinessObjects;
using com.eforceglobal.crux.BusinessObjects.DatabaseQueries;
using com.eforceglobal.crux.exceptions;

namespace com.eforceglobal.crux.BusinessFacades
{
    public class CompanyFacade
    {
        public List<CompanyInformation> GetCompanies()
        {
            return GetAllCompaniesEnumerableList().ToList();
        }

        public IEnumerable<CompanyInformation> GetAllCompaniesEnumerableList()
        {
            Queries query = new Queries();
            IEnumerable<CompanyInformation> companies =
                from companyList in query.GetCompanies()
                select ClassConverter.CopyCompanyToCompanyInformation(companyList);
            return companies;
        }

        public CompanyInformation GetCompany(int CompanyID)
        {
            if (CompanyID == 0) return new CompanyInformation();
            Queries locationQuery = new Queries();
            Company company = locationQuery.GetCompany(CompanyID);
            return ClassConverter.CopyCompanyToCompanyInformation(company);
        }

        public List<CompanyInformation> GetExternalCompanies()
        {
            Queries companyQuery = new Queries();
            IEnumerable<CompanyInformation> companies = from allcompanies in companyQuery.GetCompaniesByType("ExternalCompany")
                                                        select ClassConverter.CopyCompanyToCompanyInformation(allcompanies);
            return companies.ToList();

        }

        public bool IsCompanyInternal(int CompanyID)
        {
            if (GetCompany(CompanyID).CompanyType == "InternalCompany")
                return true;
            return false;
        }

        public bool IsCompanyExternal(int CompanyID)
        {
            if (GetCompany(CompanyID).CompanyType == "ExternalCompany")
                return true;
            return false;
        }

        public List<CompanyInformation> GetInternalCompanies()
        {
            Queries companyQuery = new Queries();
            IEnumerable<CompanyInformation> companies = from allcompanies in companyQuery.GetCompaniesByType("InternalCompany")
                                                        select ClassConverter.CopyCompanyToCompanyInformation(allcompanies);
            return companies.ToList();
        }

        public void SaveCompany(CompanyInformation company)
        {
            CheckValidity(company);
            Queries query = new Queries();
            Company dbCompany = new Company();
            if (company.CompanyID != 0)
                dbCompany = query.GetCompany(company.CompanyID);
            dbCompany = ClassConverter.CopyCompanyInformationToCompany(company, dbCompany);
            if (company.CompanyID != 0)
                query.SubmitChanges();
            else
                query.InsertCompany(dbCompany);
        }

        private void CheckValidity(CompanyInformation company)
        {

            if (DoesCompanyExist(company))
                throw CruxExceptionManager.CreateNewDatabaseException("901");

        }

        public bool DoesCompanyExist(CompanyInformation company)
        {
            Queries query = new Queries();
            Company dbCompany = query.GetCompany(company.CompanyName);
            if (dbCompany != null && dbCompany.CompanyID != company.CompanyID)
                return true;
            return false;
        }

        public void DeleteCompany(int CompanyID)
        {
            Queries query = new Queries();

            Company company = query.GetCompany(CompanyID);
            IEnumerable<Department> departments = from departmentlist in company.Departments
                                                  select departmentlist;
            query.DeleteDepartments(departments, false);
            query.DeleteCompany(company, false);
            query.SubmitChanges();
        }

        public List<CompanyInformation> GetCompaniesRepresentedBy(int RepresentativeId)
        {
            return GetCompaniesRepresentedByEnumerableList(RepresentativeId).ToList();
        }

        public IEnumerable<CompanyInformation> GetCompaniesRepresentedByEnumerableList(int RepresentativeId)
        {
            Queries query = new Queries();
            IEnumerable<CompanyInformation> companies = from allcompanies in query.GetCompaniesRepresentedBy(RepresentativeId)
                                                        select ClassConverter.CopyCompanyToCompanyInformation(allcompanies);
            return companies;
        }

        public CompanyInformation GetCompany(string CompanyName)
        {
            Queries query = new Queries();
            Company company = query.GetCompany(CompanyName);
            return ClassConverter.CopyCompanyToCompanyInformation(company);
        }
    }
}
